package cn.cmcc.intertelecom.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.bean.UserExample;
import cn.cmcc.intertelecom.bean.UserExample.Criteria;
import cn.cmcc.intertelecom.dao.UserMapper;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	public List<User> getUsersByDeptId(Integer deptId) {
		UserExample example=new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andDeptIdEqualTo(deptId);
		List<User> list = userMapper.selectByExample(example);
		return list;
	}

	public List<User> selectAll() {
		UserExample example=new UserExample();
		example.setOrderByClause("id");
		return userMapper.selectByExampleWithDept(example);
	}

	public void savePerson(User user) {
		userMapper.insertSelective(user);
		
	}

	public User getUserById(Integer id) {
		
		return userMapper.selectByPrimaryKeyWithDept(id);
	}

	/**
	 * 更新用户信息
	 * @param user
	 */
	public void updatePerson(User user) {
		userMapper.updateByPrimaryKeySelective(user);
		
	}

	/**
	 * 批量删除数据
	 * @param ids
	 */
	public void deleteBatch(List<Integer> ids) {
		UserExample example=new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(ids);
		int i = userMapper.deleteByExample(example);
		
	}
	
	/**
	 * 根据id 删除一条数据
	 * @param parseInt
	 */
	public void deleteDept(int id) {
		userMapper.deleteByPrimaryKey(id);
		
	}

	/**
	 * 根据已知条件查询 人员
	 * @param user
	 * @return
	 */
	public List<User> selectUserByRequirement(User user) {
		UserExample example=new UserExample();
		Criteria criteria = example.createCriteria();
		//System.out.println(user.getDeptId());
		if(user.getDeptId() != null && !user.getDeptId().equals("") ){
			criteria.andDeptIdEqualTo(user.getDeptId());
		}
		if(user.getId() != null && !user.getId().equals("")){
			criteria.andIdEqualTo(user.getId());
		}
		if(user.getUsername() !=null && !user.getUsername().equals("")){
			criteria.andUsernameEqualTo(user.getUsername());
		}
		if(user.getUserType() !=null && !user.getUserType().equals("")){
			criteria.andUserTypeEqualTo(user.getUserType());
		}

		return userMapper.selectByExampleWithDept(example);
	}

	/**
	 * 仅仅获取所有的用户
	 * @return
	 */
	public List<User> selectAllJustUser() {
		
		return userMapper.selectByExample(null);
	}
}
